package com.cwf.auth.controller;

import com.cwf.auth.domain.Menu;
import com.cwf.auth.query.MenuQuery;
import com.cwf.auth.service.IMenuService;
import com.cwf.basic.annotation.AutoScanPermission;
import com.cwf.basic.util.AjaxResult;
import com.cwf.basic.util.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
@Api(value = "菜单的API", description = "菜单相关的CRUD功能")
@AutoScanPermission(name = "菜单管理", desc = "菜单管理接口")
public class MenuController {
    @Autowired
    public IMenuService menuService;


    /**
     * 保存和修改公用的
     *
     * @param menu 传递的实体
     * @return Ajaxresult转换结果
     */

    @PutMapping
    @ApiOperation(value = "新增或修改菜单")
    @AutoScanPermission(name = "新增或修改菜单", desc = "新增或修改菜单接口")
    public AjaxResult addOrUpdate(@RequestBody Menu menu) {
        try {
            if (menu.getId() != null)
                menuService.update(menu);
            else
                menuService.insert(menu);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    @ApiOperation(value = "根据ID删除指定菜单")
    @AutoScanPermission(name = "根据ID删除指定菜单", desc = "根据ID删除指定菜单接口")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            menuService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询指定菜单")
    @AutoScanPermission(name = "根据ID查询指定菜单", desc = "根据ID查询指定菜单接口")
    public AjaxResult queryById(@PathVariable("id") Long id) {
        try {
            Menu menu = menuService.queryById(id);
            return AjaxResult.success(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！" + e.getMessage());
        }
    }

    @GetMapping("/tree")
    @ApiOperation(value = "角色分配菜单列表")
    @AutoScanPermission(name = "角色分配菜单列表", desc = "角色分配菜单列表接口")
    public AjaxResult tree() {
        try {
            List<Menu> menus = menuService.tree();
            return AjaxResult.success(menus);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！" + e.getMessage());
        }
    }

    /**
     * 获得父级菜单列表
     *
     * @return
     */
    @GetMapping("/parent")
    @ApiOperation(value = "获得父部门列表")
    @AutoScanPermission(name = "获得父部门列表", desc = "获得父部门列表接口")
    public AjaxResult parentTree() {
        try {
            List<Menu> menus = menuService.parentTree();
            return AjaxResult.success(menus);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！" + e.getMessage());
        }
    }

    /**
     * 查看所有的菜单信息
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查看所有的菜单信息")
    @AutoScanPermission(name = "查看所有的菜单信息", desc = "查看所有的菜单信息接口")
    public AjaxResult queryAll() {

        try {
            List<Menu> list = menuService.queryAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！" + e.getMessage());
        }
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    @ApiOperation(value = "分页查询菜单信息")
    @AutoScanPermission(name = "分页查询菜单信息", desc = "分页查询菜单信息接口")
    public AjaxResult queryPage(@RequestBody MenuQuery query) {
        try {
            PageInfo<Menu> pageList = menuService.queryPage(query);
            return AjaxResult.success(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！" + e.getMessage());
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除菜单信息")
    @AutoScanPermission(name = "批量删除菜单信息", desc = "批量删除菜单信息接口")
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        try {
            menuService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除数据失败！" + e.getMessage());
        }
    }
}
